package me.yangbajing

import net.liftweb.http.LiftRules
import net.liftweb.sitemap.{**, Menu, SiteMap}
import net.liftweb.common.Full

import me.yangbajing.web.{W, MeURI}
import me.yangbajing.helper.RdbHelpers
import me.yangbajing.web.rest.{ResourceRest, DocumentRest}

object MeRules {
  def init() {
    RdbHelpers.registerSquerylSession()

    LiftRules.addToPackages("me.yangbajing.web")
    LiftRules.ajaxRetryCount = Full(0)
    LiftRules.statelessDispatch append DocumentRest
    LiftRules.statelessDispatch append ResourceRest
    LiftRules.statelessRewrite append MeURI.rewrite
    LiftRules.setSiteMap(sitemap)
    LiftRules.unloadHooks.append(() => RdbHelpers.shutdown())
  }

  val sitemap = SiteMap(
    Menu("document") / "h" / "document" submenus(
      Menu("document-index") / "h" / "document" / "index",
      Menu("document-show") / "h" / "document" / "show",
      Menu("document-edit") / "h" / "document" / "edit" >> W.testSession
      ),
    Menu("tag") / "h" / "tag" / **,
    Menu("category") / "h" / "category" / **,
    Menu("image") / "h" / "image" / **,
    Menu("user") / "h" / "user" submenus(
      Menu("user-forgot_password") / "h" / "user" / "forgot_password",
      Menu("user-index") / "h" / "user" / "index",
      Menu("user-sign_in") / "h" / "user" / "sign_in",
      Menu("user-sign_up") / "h" / "user" / "sign_up",
      Menu("user-detail") / "h" / "user" / "detail" >> W.testSession,
      Menu("user-change_password") / "h" / "user" / "change_password" >> W.testSession,
      Menu("user-category") / "h" / "user" / "category" >> W.testSession,
      Menu("user-comments") / "h" / "user" / "comments" >> W.testSession,
      Menu("user-sign_out") / "h" / "user" / "sign_out" >> W.signOut
      ),
    Menu("index") / "h" / "index",
    Menu("about") / "h" / "about",
    Menu("404") / "h" / "404",
    Menu("500") / "h" / "500",
    Menu("privacy") / "h" / "privacy",
    Menu("terms") / "h" / "terms"
  )
}
